#!/bin/bash
set -e

svn_master='http://eathena-project.googlecode.com/svn/'
svn_slave1='https://eathena.svn.sourceforge.net/svnroot/eathena/'

rev_master=`svn log -q -rHEAD "$svn_master" | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`
rev_slave1=`svn log -q -rHEAD "$svn_slave1" | sed -ne '/^r[0-9]/{ s/^r//; s/ .*//; p; q; }'`

if [ "$rev_master" -gt "$rev_slave1" ]; then
	clear
	echo "Updating mirror from $rev_slave1 to $rev_master ..."
	
	adminrepo --checkout svn < ./.answer | tail -n 4
	mkdir -p /svnroot/eathena/hooks/
	cp -f ./.pre-revprop-change /svnroot/eathena/hooks/pre-revprop-change
	
	svnsync sync file:///svnroot/eathena/
	
	php ./svn_history.php | mysql --host=MYSQL_HOST --user=MYSQL_USER --password='MYSQL_PASSWORD' MYSQL_DATABASE
	
	adminrepo --save svn < ./.answer
fi

if [ "$rev_master" = "$rev_slave1" ]; then
	echo "Mirror is up to date at revision $rev_slave1"
fi
